package com.finger.forest.utils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author jzj
 * @date 2020/10/5
 * @desc
 */
public class DateUtil {
    public static final String DATE_FORMAT_HH_mm = "HH:mm";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_FORMAT_STR = "yyyy/MM/dd";
    public static final String DATE_FORMAT_MM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_SS = "yyyyMMddHHmmss";

    public static Date getToday() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    /**
     * 将时间戳转换为时间 (只有小时和分钟)
     */
    public static String stampToHourAndMinuteUseMillisecond(long millisecond) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT_HH_mm);
        Date date = new Date(millisecond);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 (只有小时和分钟)
     */
    public static String stampToDate(long millisecond, String formatStr) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
        Date date = new Date(millisecond);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将当前时间格式化
     */
    public static String dateToStr(String formatStr) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
        return simpleDateFormat.format(date);
    }

    /**
     * date转string
     */
    public static String dateToStr(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
        return simpleDateFormat.format(date);
    }

    /**
     * 星期转换
     * @param day
     * @return
     */
    public static String getDayName(int day){
        String dayName = "";

        switch(day){
            case 1 :
                dayName = "日";
                break;
            case 2 :
                dayName = "一";
                break;
            case 3 :
                dayName = "二";
                break;
            case 4 :
                dayName = "三";
                break;
            case 5 :
                dayName = "四";
                break;
            case 6 :
                dayName = "五";
                break;
            case 7 :
                dayName = "六";
                break;
            default :
                dayName = "";
        }
        return dayName;
    }

    //日期转换时间戳 毫秒
    public static String dateToStamp(String s) throws Exception {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long time = date.getTime();
        res = String.valueOf(time);
        return res;
    }

    //计算相差多少小时
    public static double differHours(String startTime,String endTime)throws  Exception{
        double hour=0;
        try {
            String startStamp=dateToStamp(startTime);//登记时间戳
            String endStamp=dateToStamp(endTime);//整改期限时间戳
            BigDecimal startStampInt=BigDecimal.valueOf(Long.valueOf(startStamp));
            BigDecimal endStampInt=BigDecimal.valueOf(Long.valueOf(endStamp));
            BigDecimal apartStamp=endStampInt.subtract(startStampInt);
            System.out.println("时间差："+apartStamp);
            BigDecimal hourStamp=BigDecimal.valueOf(Long.valueOf(1000*60*60));
            BigDecimal hours=apartStamp.divide(hourStamp,4, BigDecimal.ROUND_HALF_DOWN);//相差几小时
            hour=hours.doubleValue();
        }catch (Exception e){
            e.printStackTrace();
        }
        return hour;
    }

    public static void main(String[] args) {
        try {
            /*double h = differHours("2021-03-12 13:01:22","2021-03-12 14:08:22");
            System.out.println("小时："+h);*/
            System.out.println(String.valueOf(new Date().getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
